#!/usr/bin/perl
######################################################################
# Script for combining 2 plots into a single postscript document
# -- this generates a latex script based on the command line inputs
#    and several hardcoded variables. The latex script is then run to
#    generate the postscript output.
######################################################################

#####################################################################
# PAGE LAYOUT ... ??
#
# |----------|
# | G1    G2 |
# |          |
# |          |
# |          |
# |          |
# | G3    G4 |
# |----------|
#
#####################################################################


######################################################################
# HARDCODED VARIABLES
######################################################################
$PLOT_NAME_ROOT="data_dirty_window_clean";
$FILE1_ROOT="PLOT_window";
$FILE2_ROOT="PLOT_clean";
$FILE3_ROOT="PLOT_data";
$FILE4_ROOT="PLOT_dirty";
######################################################################

$numArgs = $#ARGV + 1;
print STDERR "---------------------------------------------\n";
print STDERR "ARG1 (DIR)       = $ARGV[0]\n";
print STDERR "ARG2 (ID)        = $ARGV[1]\n";
print STDERR "---------------------------------------------\n";

#usage info
if ($numArgs != 2) {
    print STDERR "ERROR: command line args, ($numArgs/2)\n";
    print STDERR "USAGE: PLOT_data_clean_2x2.pl [DIR] [ID]\n"; 
    print STDERR "---------------------------------------------------\n";
    print STDERR "#
# |----------|
# | F2    F1 |
# |          |
# |          |
# |          |
# |          |
# | F3    F4 |
# |----------|
#\n";
    exit(1);
}

$DIR=$ARGV[0];
$ID=$ARGV[1];

$FILE1="${FILE1_ROOT}-${ID}.ps";
$FILE1_PATH="$DIR/${FILE1}";
$FILE2="${FILE2_ROOT}-${ID}.ps";
$FILE2_PATH="$DIR/${FILE2}";
$FILE3="${FILE3_ROOT}-${ID}.ps";
$FILE3_PATH="$DIR/${FILE3}";
$FILE4="${FILE4_ROOT}-${ID}.ps";
$FILE4_PATH="$DIR/${FILE4}";

if ( (-e $FILE1_PATH) && (-e $FILE2_PATH) && (-e $FILE3_PATH) && (-e $FILE4_PATH)) {
}
else {
    print STDERR "ERROR: Files required by PLOT_data_clean_2x2.pl missing! ...\n";
    exit;
}


$PLOT_NAME="$PLOT_NAME_ROOT-$ID";

$PLOT_TEX="${PLOT_NAME}.tex";
$PLOT_DVI="${PLOT_NAME}.dvi";
$PLOT_PS="PLOT_${PLOT_NAME}.ps";
$PLOT_LOG="${PLOT_NAME}.log";
$PLOT_AUX="${PLOT_NAME}.aux";
$PLOT_INFO="${PLOT_NAME}.info";
$PLOT_RUN="${PLOT_NAME}.run";

print STDERR "=============================================\n";
print STDERR "DIR   = $DIR\n";
print STDERR "FILE1 = $FILE1\n";
print STDERR "FILE2 = $FILE2\n";
print STDERR "FILE3 = $FILE3\n";
print STDERR "FILE4 = $FILE4\n";
print STDERR "=============================================\n";
print STDERR "TEX FILE = $PLOT_TEX\n";
print STDERR "DVI FILE = $PLOT_DVI\n";
print STDERR "LOG FILE = $PLOT_LOG\n";
print STDERR "AUX FILE = $PLOT_AUX\n";
print STDERR "INFO FILE= $PLOT_INFO\n";
print STDERR "---------------------------------------------\n";
print STDERR "PS  FILE = $PLOT_PS\n";
print STDERR "=============================================\n";
print STDERR "RUN FILE = $PLOT_RUN\n";
print STDERR "=============================================\n";

print STDERR "=-> writing info file to ${DIR}/${PLOT_INFO}\n";
open(INFOFILE,">${DIR}/${PLOT_ARGS}");
print INFOFILE "1(DIR)     = ${DIR}\n";
print INFOFILE "2(ID)      = ${ID}\n";
print INFOFILE "=(FILE1)   = ${FILE1}\n";
print INFOFILE "=(FILE2)   = ${FILE2}\n";
print INFOFILE "=(FILE3)   = ${FILE3}\n";
print INFOFILE "=(FILE4)   = ${FILE4}\n";
print INFOFILE "=(NAME)    = ${PLOT_NAME}\n";
print INFOFILE "=(RUN FILE)= ${PLOT_RUN}\n";
print INFOFILE "=(PS FILE) = ${PLOT_PS}\n";
close(INFOFILE);


open(TFILE,">${DIR}/${PLOT_TEX}");

print TFILE "\\documentclass[11pt,a4paper]{article}\n";
print TFILE "%-----------------------------------------------------------\n";
print TFILE "%\n";
print TFILE "% latex file for:\n";
print TFILE "% DIR: $DIR\n";
print TFILE "% FILE1: $FILE1\n";
print TFILE "% FILE2: $FILE2\n";
print TFILE "% FILE3: $FILE3\n";
print TFILE "% FILE4: $FILE4\n";
print TFILE "%\n";
print TFILE "%-----------------------------------------------------------\n";
print TFILE "%Margin setup                     \n";
print TFILE "\\setlength{\\voffset}{0.1in}     \n";
print TFILE "\\setlength{\\headheight}{0in}    \n";
print TFILE "\\setlength{\\headsep}{0in}       \n";
print TFILE "\\setlength{\\textheight}{9.8in}  \n";
print TFILE "\\setlength{\\topmargin}{-1in}  \n";
print TFILE "\\setlength{\\topskip}{0in}       \n";
print TFILE "\\setlength{\\oddsidemargin}{-1in}  \n";
print TFILE "\\setlength{\\evensidemargin}{-0.4in} \n";
print TFILE "\\pagestyle{empty}  %no page numbering\n";
print TFILE "\\usepackage{graphics} \n";
print TFILE "\\usepackage{sidecap} \n";
print TFILE "%----------------------------------------------------------- \n";
print TFILE "%\n";
print TFILE "\\begin{document} \n";
print TFILE "%\n";
print TFILE "%----------------------------------------------------------- \n";
print TFILE "\\begin{figure} \n";
print TFILE "\\begin{center} \n";
print TFILE "\\begin{tabular}{cc} \n";
print TFILE "\\resizebox{100mm}{!}{\\includegraphics{$DIR/$FILE1}} & \n";
print TFILE "\\resizebox{100mm}{!}{\\includegraphics{$DIR/$FILE2}} \\\\ \n";
print TFILE "\\resizebox{100mm}{!}{\\includegraphics{$DIR/$FILE3}} & \n";
print TFILE "\\resizebox{100mm}{!}{\\includegraphics{$DIR/$FILE4}} \\\\ \n";
print TFILE "\\end{tabular} \n";
print TFILE "\\end{center} \n";
print TFILE "\\end{figure} \n";
print TFILE "%----------------------------------------------------------- \n";
print TFILE "%\n";
print TFILE "\\end{document} \n";
close(TFILE);

print STDERR "=-> writing info file to ${DIR}/${PLOT_RUN}\n";
open(RUNFILE,">${DIR}/${PLOT_RUN}");
print RUNFILE "latex ${DIR}/${PLOT_TEX}\n";
print RUNFILE "dvips ${PLOT_DVI}\n";
print RUNFILE "mv ${PLOT_NAME}.ps ${DIR}/${PLOT_PS}\n";
print RUNFILE "rm ${PLOT_LOG} ${PLOT_AUX} ${PLOT_DVI}\n";
print RUNFILE "echo \"---------------------------------------------\"\n";
print RUNFILE "echo \"gv ${DIR}/${PLOT_PS} &\"\n";
print RUNFILE "echo \"---------------------------------------------\"\n";
close(RUNFILE);

print STDERR "================================================\n";
print STDERR "-Done creating latex script!\n";
print STDERR "-Running the script via the runfile\n";
system("sh ${DIR}/${PLOT_RUN}");
print STDERR "-plot complete.\n";
print STDERR "================================================\n";
